//
//  BYLoginVC.m
//  ZhiHuiLiJiang
//
//  Created by 胡忠诚 on 2018/11/20.
//  Copyright © 2018 WisdomLijiang. All rights reserved.
//登录的VC

#import "BYLoginVC.h"
#import "BYLoginView.h"//登录的View

@interface BYLoginVC ()
/**登录的View*/
@property(nonatomic,strong)BYLoginView *loginView;

@end
@implementation BYLoginVC
#pragma mark- init初始化
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self addSubViewClick];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden=YES;
    if (!self.tabBarController.tabBar.isHidden) {
        self.tabBarController.tabBar.hidden = YES;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)setupUI{
    self.view.backgroundColor = [UIColor whiteColor];
    _loginView = [[BYLoginView alloc]init];
    _loginView.frame = CGRectMake(0, BY_StatusBar_H, BY_ScreenWidth, BY_ScreenHeight-BY_StatusBar_H-BY_Bot_H);
    [self.view addSubview:_loginView];
}
- (void)addSubViewClick{
    BY_kWeakSelf(ws);
    _loginView.clickBackBtn = ^{//点击了返回按钮
        [ws backBtnClicked];
    };
    _loginView.clickSendLoginVeriCodeBtn = ^(NSString *phoneNumStr) {//点击了获取验证码
        [ws requestLoginVeriCode:phoneNumStr];
    };
    _loginView.clickLoginBtn = ^(NSDictionary * _Nonnull dic) {//点击了登录按钮
        [ws requestLoginWithParams:dic];
    };
    _loginView.clickAgreementBtn = ^{//点击了协议
        for (BYSetModel *models in APPDELEGATE.SetDataArray) {
            if ([models.setting_name isEqualToString:@"sys-privacypolicy-url"]) {
                NSString *urlStr = models.setting_value;
                BYWebVC *VC = [[BYWebVC alloc]init];
                VC.url = urlStr;
                [ws.navigationController pushViewController:VC animated:YES];
            }
        }
    };
}
- (void)impleLoginBlock{//登录成功的回调
    if (self.loginComplete) {
        self.loginComplete();
    }
}
- (void)backBtnClicked{//默认返回按钮的点击事件，默认是返回，子类可重写
    if (self.presentingViewController) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

#pragma mark- 网络请求
- (void)requestLoginVeriCode:(NSString *)phoneStr{//请求验证码接口
    NSDictionary *dict = @{@"mobile":phoneStr};
    [BYNetworking requestGetOrPostWithType:GET url:LJURL_sendSms params:dict success:^(NSInteger code, NSString * _Nonnull msg, id  _Nonnull data) {
        [self.loginView.sendVeriCodeBtn startCheckCodeCountdown:60];
    } fail:^(NSError * _Nonnull error) {
        self.loginView.sendVeriCodeBtn.enabled = YES;
    } showHUD:YES];
}
- (void)requestLoginWithParams:(NSDictionary *)dict{
    NSLog(@"点击了登录，参数为：%@",dict);
    [userManager requestLoginParams:dict completion:^(BOOL success, NSString * _Nonnull msg, id  _Nonnull BODY) {
        if (success) {
            [self impleLoginBlock];
            [self backBtnClicked];
        }
    }];
}


@end
